<?php

namespace app\v1\model;

use think\Model;
use think\Db;

/**
 * Class Comment
 * @package app\v1\model
 * 模块说明：这个类用来处理每日点评的数据处理
 */

class Comment extends Base
{
    //每日点评列表
    public function everydayCommentList(){
        $userInfo=getUserInfo();
        $map['c.effect']=1;
        $map['c.active']=1;
        $map['c.org_id']=$userInfo['orgId'];
        $map['c.shop_id']=$userInfo['schoolId'];
        $everyDayCommentList=Db::name('comments')->alias('c')->join('users u','c.append_user_id=u.id','LEFT')->join('integral_user_relation iur','c.id=iur.comments_id','LEFT')
            ->join('integral i','iur.integral_id=i.id','LEFT')->join('integral_value iv','i.integral=iv.integral','LEFT')->where($map)
            ->field('c.id as commentId,u.headimgurl as avatar,u.name,c.append_time as publishTime,c.content,c.files as pictures,iv.value as integral,i.type as integralType,c.record as recordUrl,c.record_time as recordTime')->distinct(true)->order('c.id desc')->limit($this->pageCount)->page($this->currentPage)->select();
        foreach($everyDayCommentList as $key=>$value){
                $everyDayCommentList[$key]['pictures']=explode(',',$value['pictures']);
                $everyDayCommentList[$key]['markList']=$this->getMarkList($value['commentId']);
                $everyDayCommentList[$key]['toChildList']=$this->getTheStudentList($value['commentId']);
                $everyDayCommentList[$key]['pointList']=$this->getPointList($value['commentId']);
        }
        $data=array('current_page'=>$this->currentPage,'everyDayCommentList'=>$everyDayCommentList);
        renderData(0,'ok',$data);
    }
    //获取评分（markList）的五个值
    public function getMarkList($commentId){
        $map['effect']=1;
        $map['active']=1;
        $map['comments_id']=$commentId;
        $markList=Db::name('Comments_marker')->where($map)->field('marker_type,marker_number')->select();
        return $markList;
    }
    //获取@的学生列表
    public function getTheStudentList($commentId){
        $map['cr.comments_id']=$commentId;
        $map['cr.effect']=1;
        $map['cr.active']=1;
        $childList=Db::name('Comments_relation')->alias('cr')->join('users u','cr.student_id=u.id','LEFT')->where($map)->field('cr.student_id as childId,u.name as childName')->select();
        return $childList;
    }
    //获取点赞的列表
    public function getPointList($commentId){
        $map['ca.type']=1;
        $map['ca.comments_id']=$commentId;
        $map['ca.effect']=1;
        $map['ca.active']=1;
        $pointList=Db::name('Comments_action')->alias('ca')->join('users u','ca.user_id=u.id')->where($map)->field('u.id as userId,u.name,u.headimgurl as avatar')->select();
        return $pointList;
    }

    //点赞和取消点赞操作type=0,点赞，1取消点赞
    public function setCommentPoint($commentId,$type){
        try{
        $userInfo=getUserInfo();
        checkSubmit($userInfo['parentId'],'setCommentPoint'.$type,1);
        $this->checkCommentId($commentId);
        $map['user_id']=$userInfo['parentId'];
        $map['comments_id']=$commentId;
        $map['type']=1;
        $res=$this->hasPointRecord($commentId,$userInfo['parentId']);
        $isPoint=$this->isPoint($commentId,$userInfo['parentId']);
        if($type==0){
            if($res){

                if($isPoint){
                    renderInfo(400001,'已经点过赞了哦');
                }
                $data=array();
                $data['effect']=1;
                $data['active']=1;
                $rs=Db::name('Comments_action')->where($map)->update($data);
            }else{
                $data=array();
                $data['append_time']=time();
                $data['append_user_id']=$userInfo['parentId'];
                $data['modify_time']=time();
                $data['modify_user_id']=$userInfo['parentId'];
                $data['effect']=1;
                $data['active']=1;
                $data['comments_id']=$commentId;
                $data['user_id']=$userInfo['parentId'];
                $data['type']=1;
                $rs=Db::name('Comments_action')->insert($data);
            }
        }else if($type==1){
            if(!$isPoint){
                renderInfo(400001,'还未点过赞哦');
            }
            $data=array();
            $data['effect']=0;
            $data['active']=0;
            $rs=Db::name('Comments_action')->where($map)->update($data);

        }
        if(!empty($rs)){
            renderInfo(0,'操作成功');
        }else{
            renderInfo(400008,'系统繁忙,请稍后重试');
        }

        }catch (\Exception $e){
            renderInfo(400008,'系统繁忙,请稍后重试');
        }



    }

    //检查commentId是否存在
    public function checkCommentId($commentId){
        $map['effect']=1;
        $map['active']=1;
        $map['id']=$commentId;
        $res=Db::name('Comments')->where($map)->find();
        if(empty($res)){
           renderInfo(400001,'点评记录不存在哦');
        }
    }
    //检查是否已经有点赞记录
    public function hasPointRecord($commentId,$userId){
            $map['comments_id']=$commentId;
            $map['user_id']=$userId;
            $map['type']=1;
            $res=Db::name('Comments_action')->where($map)->find();
            if(!empty($res)){
                return true;
            }else{
                return false;
            }
    }
    //判断是否 已经点赞
    public function isPoint($commentId,$userId){
        $map['comments_id']=$commentId;
        $map['user_id']=$userId;
        $map['type']=1;
        $map['effect']=1;
        $map['active']=1;
        $res=Db::name('Comments_action')->where($map)->find();
        if(!empty($res)){
            return true;
        }else{
            return false;
        }
    }


}
